//
//  TTDatabaseObject.h
//  Tint
//
//  Created by Kevin Wu on 5/30/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>


@class TTDatabase;

@interface TTDatabaseObject : NSObject {
    int _pk;
}

@property(nonatomic, assign) int pk;

+ (void)setCurrentDatabase:(TTDatabase *)database;
+ (TTDatabase *)currentDatabase;
- (TTDatabase *)currentDatabase;

+ (BOOL)createTable;
+ (BOOL)deleteTable;

/*
 * Insert or update fields.
 * pk=0: insert
 * pk>0 and not in database: insert
 * pk>0 and in database: update
 */
- (BOOL)save;
- (BOOL)save:(NSString *)fieldsString;
- (BOOL)saveExcept:(NSString *)exceptFieldsString;
- (BOOL)saveFields:(NSArray *)fields exceptFields:(NSArray *)exceptFields;

/*
 * Delete from database by pk.
 */
- (BOOL)destroy;


/*
 * Check whether this object is exist in database or not by pk.
 */
- (BOOL)isExist;


/*
 * Read from database by pk.
 * Note:
 *   1). Properties which not in database will not update.
 *   2). Properties which type is not support will not update. Support type: BOOL, 
 *       int, long long, double, NSString *, NSDate *.
 */
- (void)reload;


+ (NSUInteger)count;
+ (NSUInteger)countWhere:(NSString *)where, ...;

+ (BOOL)deleteAllObjects;
+ (BOOL)deleteObjectsWhere:(NSString *)where, ...;

+ (BOOL)updateAllObjectsSet:(NSString *)set;
+ (BOOL)updateObjectsSet:(NSString *)set where:(NSString *)where, ...;

+ (id)copyFirstObject;
+ (id)copyFirstObjectOrderBy:(NSString *)orderBy;
+ (id)copyFirstObjectWhere:(NSString *)where, ...;
+ (id)copyFirstObjectOrderBy:(NSString *)orderBy where:(NSString *)where, ...;
+ (id)copyFirstObjectOrderBy:(NSString *)orderBy where:(NSString *)where parameters:(NSArray *)parameters;

+ (NSArray *)copyAllObjects;
+ (NSArray *)copyObjectsOrderBy:(NSString *)orderBy;
+ (NSArray *)copyObjectsWhere:(NSString *)where, ...;
+ (NSArray *)copyObjectsOrderBy:(NSString *)orderBy where:(NSString *)where, ...;
+ (NSArray *)copyObjectsOrderBy:(NSString *)orderBy where:(NSString *)where parameters:(NSArray *)parameters;

@end
